package com.xxxx.car.audit.utils;

import com.xxxx.car.common.utils.StringUtils;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.engine.repository.ProcessDefinition;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ActivitiUtils {

    public static BigDecimal getDiscountAmount(BpmnModel bpmnModel,String definitionKey){
        // 获取对应的process
        Process process = bpmnModel.getProcessById(definitionKey);
        Collection<FlowElement> elements = process.getFlowElements();
        for(FlowElement element : elements){
            // 如果当前是箭头类型
            if(element instanceof SequenceFlow){
                SequenceFlow flow = (SequenceFlow) element;
                // 获取箭头中的条件表达式
                String conditionExpression = flow.getConditionExpression();
                // 判断条件不为空,且以${discountPrice 开头
                if(StringUtils.isNotEmpty(conditionExpression) && conditionExpression.startsWith("${discountAmount")){
                    System.out.println(conditionExpression);
                    Pattern pattern = Pattern.compile("\\d+"); // 正则表达式，匹配一个或多个数字
                    Matcher matcher = pattern.matcher(conditionExpression);
                    while (matcher.find()) {
                        // 将找到的数字添加到列表中
                        return new BigDecimal(matcher.group());
                    }
                }
            }
        }
        return BigDecimal.ZERO;
    }
}
